import { _decorator, Animation } from 'cc'
import { ENTITY_STATE_ENUM, FSM_PARAMS_TYPE_ENUM, PARAMS_NAME_ENUM } from '../../Enum'
import State from '../../Base/State'
import { initParamsNumber, initParamsTrigger, StateMachine } from '../../Base/StateMachine'
import { IdleSubStateMachine } from './IdleSubStateMachine'
import { TurnLeftSubStateMachine } from './TurnLeftSubStateMachine'
import { BlockFrontSubStateMachine } from './BlockFrontSubStateMachine'
import { TurnRightSubStateMachine } from './TurnRightSubStateMachine'
import { EntityManager } from '../../Base/EntityManager'
import { DeathSubStateMachine } from './DeathSubStateMachine'
import { AttackSubStateMachinePlayer } from './AttackSubStateMachine'
import { AirDeathSubStateMachine } from './AirDeathSubStateMachine'
const { ccclass, property } = _decorator

/**
 * Predefined variables
 * Name = PlayerStateMachine
 * DateTime = Sat Jul 09 2022 20:20:36 GMT+0800 (中国标准时间)
 * Author = Zero_Y
 * FileBasename = PlayerStateMachine.ts
 * FileBasenameNoExtension = PlayerStateMachine
 * URL = db://assets/Scripts/Player/PlayerStateMachine.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

@ccclass('PlayerStateMachine')
export class PlayerStateMachine extends StateMachine {
  // 初始化参数
  initParams() {
    this.params.set(PARAMS_NAME_ENUM.IDLE, initParamsTrigger())
    this.params.set(PARAMS_NAME_ENUM.TURN_LEFT, initParamsTrigger())
    this.params.set(PARAMS_NAME_ENUM.TURN_RIGHT, initParamsTrigger())
    this.params.set(PARAMS_NAME_ENUM.BLOCK_FRONT, initParamsTrigger())
    this.params.set(PARAMS_NAME_ENUM.BLOCK_TURN_LEFT, initParamsTrigger())
    this.params.set(PARAMS_NAME_ENUM.DIRECTION, initParamsNumber())
    this.params.set(PARAMS_NAME_ENUM.DEATH, initParamsTrigger())
    this.params.set(PARAMS_NAME_ENUM.ATTACK, initParamsTrigger())
    this.params.set(PARAMS_NAME_ENUM.AIRDEATH, initParamsTrigger())
  }

  // 初始化状态机
  initStateMachine() {
    // 人物动画，无限播放
    this.stateMachines.set(PARAMS_NAME_ENUM.IDLE, new IdleSubStateMachine(this))
    // 左转动画，播放一次
    this.stateMachines.set(PARAMS_NAME_ENUM.TURN_LEFT, new TurnLeftSubStateMachine(this))
    // 右转动画，播放一次
    this.stateMachines.set(PARAMS_NAME_ENUM.TURN_RIGHT, new TurnRightSubStateMachine(this))
    // 向前撞
    this.stateMachines.set(PARAMS_NAME_ENUM.BLOCK_FRONT, new BlockFrontSubStateMachine(this))
    this.stateMachines.set(PARAMS_NAME_ENUM.BLOCK_TURN_LEFT, new BlockFrontSubStateMachine(this))
    // 死亡
    this.stateMachines.set(PARAMS_NAME_ENUM.DEATH, new DeathSubStateMachine(this))
    // 空中死亡
    this.stateMachines.set(PARAMS_NAME_ENUM.AIRDEATH, new AirDeathSubStateMachine(this))
    // 攻击
    this.stateMachines.set(PARAMS_NAME_ENUM.ATTACK, new AttackSubStateMachinePlayer(this))
  }

  // 初始化动画
  initAnimationEvent() {
    this.animationComponent.on(Animation.EventType.FINISHED, () => {
      // 执行完动画需要恢复默认idle动画的白名单
      const whiteList = ['turn', 'block', 'attack']
      const name = this.animationComponent.defaultClip.name
      if (whiteList.some(v => name.includes(v))) {
        // 统一state入口
        this.node.getComponent(EntityManager).state = ENTITY_STATE_ENUM.IDLE
        // this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.IDLE)
      }
    })
  }

  async init() {
    // 添加动画组件
    this.animationComponent = this.addComponent(Animation)
    this.initParams()
    this.initStateMachine()
    this.initAnimationEvent()
    // 确保资源资源加载
    await Promise.all(this.waitingList)
  }
  run() {
    switch (this.currentState) {
      case this.stateMachines.get(PARAMS_NAME_ENUM.TURN_LEFT):
      case this.stateMachines.get(PARAMS_NAME_ENUM.TURN_RIGHT):
      case this.stateMachines.get(PARAMS_NAME_ENUM.BLOCK_FRONT):
      case this.stateMachines.get(PARAMS_NAME_ENUM.BLOCK_TURN_LEFT):
      case this.stateMachines.get(PARAMS_NAME_ENUM.IDLE):
      case this.stateMachines.get(PARAMS_NAME_ENUM.DEATH):
      case this.stateMachines.get(PARAMS_NAME_ENUM.AIRDEATH):
      case this.stateMachines.get(PARAMS_NAME_ENUM.ATTACK):
        if (this.params.get(PARAMS_NAME_ENUM.ATTACK).value) {
          this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.ATTACK)
        } else if (this.params.get(PARAMS_NAME_ENUM.DEATH).value) {
          this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.DEATH)
        } else if (this.params.get(PARAMS_NAME_ENUM.AIRDEATH).value) {
          this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.AIRDEATH)
        } else if (this.params.get(PARAMS_NAME_ENUM.BLOCK_FRONT).value) {
          this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.BLOCK_FRONT)
        } else if (this.params.get(PARAMS_NAME_ENUM.BLOCK_TURN_LEFT).value) {
          this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.BLOCK_TURN_LEFT)
        } else if (this.params.get(PARAMS_NAME_ENUM.TURN_LEFT).value) {
          this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.TURN_LEFT)
        } else if (this.params.get(PARAMS_NAME_ENUM.TURN_RIGHT).value) {
          this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.TURN_RIGHT)
        } else if (this.params.get(PARAMS_NAME_ENUM.IDLE).value) {
          this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.IDLE)
        } else {
          // 为了触发子状态机的改变
          this.currentState = this.currentState
        }
        break
      default:
        this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.IDLE)
    }
  }
}
